package esi.isoft2;

/**
 * 
 */

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;




/**
 * @author Ismael
 *
 */
public class GUICuenta extends JFrame{

	private int saldo = 0;
	private JLabel labelSaldo = new JLabel ("Saldo:");
	private JLabel labelCantidad = new JLabel ("0");
	private JButton bIngresar = new JButton ("Ingresar"), bRetirar  = new JButton ("Retirar");
	private JTextField t = new JTextField (20);
	Container contentPane = getContentPane();
	
	public GUICuenta (){
		init ();
		pack ();
	}
	
	public void init (){
		setTitle ("Cuenta no Desligada");
		setDefaultCloseOperation (EXIT_ON_CLOSE);
		JPanel panelBotones = new JPanel (new GridLayout (1,2));
		JPanel panelSaldo = new JPanel ();
		panelBotones.add (bIngresar);
		panelBotones.add (bRetirar);
		panelSaldo.add (labelSaldo);
		panelSaldo.add (labelCantidad);
		contentPane.add(panelSaldo, BorderLayout.NORTH);
		contentPane.add (t, BorderLayout.CENTER);
		contentPane.add (panelBotones, BorderLayout.SOUTH);
		bIngresar.addActionListener (new ActionListener (){
			public void actionPerformed (ActionEvent e)
			{
				int cantidad = Integer.parseInt(t.getText());
				ingresar (cantidad);
				labelCantidad.setText (Integer.toString (saldo));
			}
		});
		
		bRetirar.addActionListener (new ActionListener ()
		{
			public void actionPerformed (ActionEvent e)
			{
				int cantidad = Integer.parseInt(t.getText());
				retirar (cantidad);
				labelCantidad.setText (Integer.toString (saldo));
			}
		}
				
				);
		
		
		
		
	}
	public void ingresar (int cantidad){
		saldo = saldo +cantidad;
	}
	public void retirar (int cantidad){
		if (cantidad < saldo) 
			saldo = saldo - cantidad;
		else
			saldo =0;
	}
}



